package cn.donting.jme;

import cn.donting.jme.core.state.CoordinateViewState;
import cn.donting.jme.ioc.BeanInitialization;
import cn.donting.jme.texture.TextureLoader;
import com.jme3.app.*;
import com.jme3.app.state.AppState;
import com.jme3.app.state.ConstantVerifierState;
import com.jme3.asset.AssetManager;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.audio.AudioListenerState;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.system.AppSettings;
import lombok.Getter;

import java.util.List;

public class JmeApplication extends SimpleApplication {


    private Thread jmeThread;

    private Node editorNode;
    @Getter
    private Node sceneNode;

    private Camera editorCamera;

    public JmeApplication() {
        super(
                new StatsAppState(), new DebugKeysAppState(),
                new ConstantVerifierState()
        );
    }

    @Override
    public void simpleInitApp() {
        editorNode = new Node("编辑器节点");
        sceneNode = new Node("场景节点");
        rootNode.attachChild(editorNode);
        rootNode.attachChild(sceneNode);
        jmeThread = Thread.currentThread();
        setLostFocusBehavior(LostFocusBehavior.Disabled);



        List<AppState> appStates = EditorContent.getIocContainer().getBeansByTypes(AppState.class);
        for (AppState appState : appStates) {
            stateManager.attach(appState);
        }
        editorCamera=cam;
        cam=null;

        AssetManager assetManager = getAssetManager();
        Spatial translation = assetManager.loadModel("tranlation/translation.j3o");

        //初始化
        List<BeanInitialization> sceneTools = EditorContent.getIocContainer().getBeansByTypes(BeanInitialization.class);
        for (BeanInitialization initialization : sceneTools) {
            initialization.initialization(this);
        }

    }

    /**
     * 主循环
     */
    @Override
    public void simpleUpdate(float deltaTime) {
    }

    public static JmeApplication create() {
        JmeApplication jmeApplication = new JmeApplication();
        AppSettings appSettings = new AppSettings(true);
        appSettings.setVSync(false);
        appSettings.setSamples(8);
        jmeApplication.setSettings(appSettings);
        return jmeApplication;
    }

    public boolean isJmeThread() {
        return Thread.currentThread() == jmeThread;
    }

    public Node getEditorNode() {
        return editorNode;
    }

    public Camera getEditorCamera(){
        return editorCamera;
    }

}
